package com.xinzhidi.web.plot.controller;

import com.xinzhidi.common.annotation.Log;
import com.xinzhidi.common.core.controller.BaseController;
import com.xinzhidi.common.core.domain.AjaxResult;
import com.xinzhidi.common.core.domain.R;
import com.xinzhidi.common.core.domain.model.LoginUser;
import com.xinzhidi.common.core.page.TableDataInfo;
import com.xinzhidi.common.enums.BusinessType;
import com.xinzhidi.common.exception.job.TaskException;
import com.xinzhidi.common.utils.SecurityUtils;
import com.xinzhidi.common.utils.poi.ExcelUtil;
import com.xinzhidi.web.netty.utils.SecurityAdminUtils;
import com.xinzhidi.web.plot.domain.dto.CPlotAddDto;
import com.xinzhidi.web.plot.domain.dto.CPlotListDto;
import com.xinzhidi.web.plot.domain.dto.CPlotUpdDto;
import com.xinzhidi.web.plot.domain.vo.CPlotInfoVo;
import com.xinzhidi.web.plot.domain.vo.CPlotListVo;
import com.xinzhidi.web.plot.domain.vo.SelectVo;
import com.xinzhidi.web.plot.service.ICPlotService;
import com.xinzhidi.web.task.AutomaticMeterReadTask;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 小区Controller
 *
 * @author ruoyi
 * @date 2023-05-19
 */
@RestController
@Api(tags = "小区")
@RequestMapping("/plot")
public class CPlotController extends BaseController {
    @Autowired
    private ICPlotService cPlotService;

    @Autowired
    private AutomaticMeterReadTask automaticMeterReadTask;

    @ApiOperation("查询小区列表")
    @PreAuthorize("@ss.hasPermi('plot:plot:list')")
    @PostMapping("/listCPlot")
    public TableDataInfo<CPlotListVo> listCPlot(@Validated @RequestBody CPlotListDto dto) {
        List<Long> userPlotIds = SecurityAdminUtils.getUserPlotIds();
        LoginUser user = SecurityUtils.getLoginUser();
        startPagePost(dto);
        return cPlotService.selectCPlotList(dto, userPlotIds);
    }

    @ApiOperation("查询小区下拉列表")
    @PostMapping("/listCPlotSelect")
    public TableDataInfo<SelectVo> listCPlotSelect(@Validated @RequestBody CPlotListDto dto) {
        dto.setPageSize(9999);
        startPagePost(dto);
        return cPlotService.listCPlotSelect(dto);
    }

    @ApiOperation("小区列表不分页")
    @PreAuthorize("@ss.hasPermi('plot:plot:list')")
    @PostMapping("/listCPlotNoPage")
    public AjaxResult listCPlotNoPage() {
        return AjaxResult.success(cPlotService.selectlistCPlotNoPage());

    }

    @ApiOperation("导出小区列表")
    @PreAuthorize("@ss.hasPermi('plot:plot:export')")
    @Log(title = "小区", businessType = BusinessType.EXPORT)
    @PostMapping("/exportCPlot")
    public void exportCPlot(HttpServletResponse response, CPlotListDto dto) {
        List<Long> userPlotIds = SecurityAdminUtils.getUserPlotIds();
        TableDataInfo<CPlotListVo> list = cPlotService.selectCPlotList(dto, userPlotIds);
        ExcelUtil<CPlotListVo> util = new ExcelUtil<CPlotListVo>(CPlotListVo.class);
        util.exportExcel(response, list.getRows(), "小区数据");
    }

    @ApiOperation("获取小区详细信息")
    @PreAuthorize("@ss.hasPermi('plot:plot:query')")
    @GetMapping(value = "/getCPlotById")
    public R<CPlotInfoVo> getCPlotById(@RequestParam("id") Long id) {
        return R.ok(cPlotService.selectCPlotById(id));
    }

    @ApiOperation("新增小区")
    @PreAuthorize("@ss.hasPermi('plot:plot:add')")
    @Log(title = "小区", businessType = BusinessType.INSERT)
    @PostMapping("addCPlot")
    public AjaxResult addCPlot(@Validated @RequestBody CPlotAddDto dto) throws SchedulerException, TaskException {
        return toAjax(cPlotService.insertCPlot(dto));
    }

    @ApiOperation("修改小区")
    @PreAuthorize("@ss.hasPermi('plot:plot:edit')")
    @Log(title = "小区", businessType = BusinessType.UPDATE)
    @PostMapping("editCPlot")
    public AjaxResult editCPlot(@Validated @RequestBody CPlotUpdDto dto) throws SchedulerException, TaskException {
        return toAjax(cPlotService.updateCPlot(dto));
    }

    @ApiOperation("删除小区")
    @PreAuthorize("@ss.hasPermi('plot:plot:remove')")
    @Log(title = "小区", businessType = BusinessType.DELETE)
    @DeleteMapping("/delCPlot")
    public AjaxResult delCPlot(@RequestParam Long[] ids) {
        return toAjax(cPlotService.deleteCPlotByIds(ids));
    }

    @ApiOperation("测试小区定时任务")
    @GetMapping("/testtASK")
    public void testtASK(Integer plotId) {
        automaticMeterReadTask.ryMultipleParams(plotId);
    }


}
